home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / os2 / pccts.zip / SC.H < prev    next >
C/C++ Source or Header  |  1992-12-08  |  5KB  |  144 lines

  1. /* sc.h -- string C stack machine macros
  2.  *
  3.  * For use with PCCTS advanced tutorial version 1.0x
  4.  *
  5.  * SOFTWARE RIGHTS
  6.  *
  7.  * We reserve no LEGAL rights to the Purdue Compiler Construction Tool
  8.  * Set (PCCTS) -- PCCTS is in the public domain.  An individual or
  9.  * company may do whatever they wish with source code distributed with
  10.  * PCCTS or the code generated by PCCTS, including the incorporation of
  11.  * PCCTS, or its output, into commerical software.
  12.  * 
  13.  * We encourage users to develop software with PCCTS.  However, we do ask
  14.  * that credit is given to us for developing PCCTS.  By "credit", we
  15.  * mean that if you incorporate our source code into one of your
  16.  * programs (commercial product, research project, or otherwise) that you
  17.  * acknowledge this fact somewhere in the documentation, research report,
  18.  * etc...  If you like PCCTS and have developed a nice tool with the
  19.  * output, please mention that you developed it using PCCTS.  In
  20.  * addition, we ask that this header remain intact in our source code.
  21.  * As long as these guidelines are kept, we expect to continue enhancing
  22.  * this system and expect to make other tools available as they are
  23.  * completed.
  24.  *
  25.  * Terence Parr
  26.  * Purdue University
  27.  * 1989-1992
  28.  */
  29. #include <stdio.h>
  30. #include <ctype.h>
  31. #include <math.h>
  32.  
  33. /*
  34.  * The function invocation stack looks like:
  35.  *
  36.  *  |    .        |
  37.  *  |    .        |
  38.  *  |    arg        |    fp + 0        arg is "" if none specified
  39.  *  | 1st local    |    fp - 1
  40.  *  | 2nd local    |    fp - 2
  41.  *  |    ...        |
  42.  *  |    .        |
  43.  *  |    .        |
  44.  */
  45.  
  46. #define STR_SIZE        100
  47. #define STK_SIZE        200
  48.  
  49. /* define stack */
  50. typedef struct { char text[STR_SIZE]; } SCVAR;
  51. static SCVAR stack[STK_SIZE];
  52. static int sp = STK_SIZE, fp;
  53.  
  54. /* number begins with number or '.' followed by number.  All numbers
  55.  * are converted to floats before comparison.
  56.  */
  57. #define SCnumber(a)    (isdigit(a[0]) || (a[0]=='.' && isdigit(a[1])))
  58.  
  59. #define TOS            stack[sp].text
  60. #define NTOS        stack[sp+1].text
  61. #define TOSTRUE        ((strcmp(TOS, "true")==0)||(strcmp(TOS, "1")==0)        \
  62.                     ||(SCnumber(TOS)&&atof(TOS)==1.0) )
  63. #define TOSFALSE    ((strcmp(TOS, "false")==0)||(strcmp(TOS, "0")==0)        \
  64.                     ||(SCnumber(TOS)&&atof(TOS)==0.0) )
  65.  
  66. #define PUSH(a)        {if ( sp==0 ) {fprintf(stderr, "stk ovf!\n"); exit(-1);} \
  67.                     strcpy(stack[--sp].text, (a).text);}
  68.  
  69. #define SPUSH(a)    {if ( sp==0 ) {fprintf(stderr, "stk ovf!\n"); exit(-1);} \
  70.                     strcpy(stack[--sp].text, a);}
  71.  
  72. #define LPUSH(a)    {if ( sp==0 ) {fprintf(stderr, "stk ovf!\n"); exit(-1);} \
  73.                     stack[--sp] = stack[fp-a];}
  74.  
  75. #define CALL(f)        {f();}
  76.  
  77. #define POP            stack[sp++]
  78.  
  79. #define LOCAL        {if ( sp==0 ) {fprintf(stderr, "stk ovf!\n"); exit(-1);} \
  80.                     stack[--sp].text[0] = '\0';}
  81.  
  82. #define BRF(lab)    if ( TOSFALSE ) {POP; goto lab;} else POP;
  83. #define BRT(lab)    if ( TOSTRUE ) {POP; goto lab;} else POP;
  84. #define BR(lab)        goto lab
  85.  
  86. #define PRINT        printf("%s", POP.text)
  87.  
  88. #define RETURN        {strcpy(stack[fp].text, TOS); END; return;}
  89.  
  90. #define STORE(v)    {v = POP;}
  91. #define LSTORE(off)    {stack[fp-off] = POP;}
  92.  
  93. /* operators */
  94.  
  95. #define EQ            {char *a,*b; float c,d;                                    \
  96.                     a = POP.text; b = POP.text;                                \
  97.                     if ( SCnumber(a) && SCnumber(b) ) {                        \
  98.                         c=atof(a); d=atof(b);                                \
  99.                         if ( c == d ) {SPUSH("true");}                        \
  100.                         else SPUSH("false");                                \
  101.                     }                                                        \
  102.                     else if ( strcmp(a, b)==0 ) {SPUSH("true");}            \
  103.                          else SPUSH("false");}
  104. #define NEQ            {SCVAR a,b; float c,d;                                    \
  105.                     a = POP.text; b = POP.text;                                \
  106.                     if ( SCnumber(a) && SCnumber(b) ) {                        \
  107.                         c=atof(a); d=atof(b);                                \
  108.                         if ( c != d ) {SPUSH("true");}                        \
  109.                         else SPUSH("false");                                \
  110.                     }                                                        \
  111.                     else if ( strcmp(a, b)!=0 ) {SPUSH("true");}            \
  112.                          else SPUSH("false");}
  113. #define ADD            {SCVAR c; float a,b;                                    \
  114.                     if ( !SCnumber(NTOS) || !SCnumber(TOS) ) {                 \
  115.                         strncat(NTOS, TOS, STR_SIZE - strlen(NTOS));        \
  116.                         sp++;                                                \
  117.                     } else {                                                \
  118.                         a=atof(POP.text); b=atof(POP.text);                    \
  119.                         sprintf(c.text, "%f", a+b); PUSH(c);                \
  120.                     }}
  121. #define SUB            {SCVAR c; float a,b; a=atof(POP.text); b=atof(POP.text); \
  122.                     sprintf(c.text, "%f", b-a); PUSH(c);}
  123. #define MUL            {SCVAR c; float a,b; a=atof(POP.text); b=atof(POP.text); \
  124.                     sprintf(c.text, "%f", a*b); PUSH(c);}
  125. #define DIV            {SCVAR c; float a,b; a=atof(POP.text); b=atof(POP.text); \
  126.                     sprintf(c.text, "%f", b/a); PUSH(c);}
  127. #define NEG            {SCVAR c; float a; a=atof(POP.text); \
  128.                     sprintf(c.text, "%f", -a); PUSH(c);}
  129. #define DUP            {if ( sp==0 ) {fprintf(stderr, "stk ovf!\n"); exit(-1);} \
  130.                     stack[sp-1] = stack[sp]; --sp;}
  131.  
  132. #define BEGIN        int save_fp = fp; fp = sp
  133. #define END            sp = fp; fp = save_fp;
  134.  
  135. main(argc, argv)
  136. int argc;
  137. char *argv[];
  138. {
  139.     if ( argc == 2 ) {SPUSH(argv[1]);}
  140.     else SPUSH("");
  141.     CALL(_main);
  142.     POP;
  143. }
  144.